<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\common\config\model\ConfigStore;
use app\common\config\model\ConfigStoreHall;
use app\common\config\model\ConfigStoreMenu;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;

class Store
{
    protected $request = null;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function index(): \think\response\Json
    {

        $post = $this->request->post();
        if (empty($post['id'])) return error([], '参数错误');

        $storeModel = new ConfigStore();

        // 门店信息
        $store = $storeModel->withoutField('status,sort,create_time,update_time,delete_time')->where('id',$post['id'])->find();
        $result['store'] = $store;

        // 获取该门店所有的厅
        $hallModel = new ConfigStoreHall();
        $halls = $hallModel->withoutField('status,sort,create_time,update_time,delete_time')->where('store_id',$post['id'])->select();
        foreach ($halls as $hall) {
            $hall->append(['shapes']);
        }
        $result['halls'] = $halls;

        // 获取该门店所有的菜单
        $menuModel = new ConfigStoreMenu();
        $menus = $menuModel->withoutField('status,sort,create_time,update_time,delete_time')->where('store_id',$post['id'])->select();
        $result['menus'] = $menus;


        return success($result, '获取数据成功');
    }

    public function halls(): \think\response\Json
    {
        $post = $this->request->post();
        if (empty($post['sid'])) return error([], '参数错误');

        $where = [
            'store_id' => $post['sid'],
            'status' => 1,
        ];

        $hallModel = new ConfigStoreHall();
        $withoutField = 'status,sort,create_time,update_time,delete_time';
        try {
            $halls = $hallModel->withoutField($withoutField)->where($where)->order('sort desc,id')->select();
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            $halls = [];
        }


        return success(['halls'=>$halls], '获取数据成功');
    }
}
